<?php
	/*
		Shikihime Main Helper
		
		
		@author tidusvn05@gmail.com
		@copyright All rights reserved - 2012
	*/
	
	require('include/lang/english.php');
	require('helper/MainHelper.php');
	require('conf.inc.php');
	
	
	session_start();
	
	/*
		Get the specified view and replace the php variables
	*/	
	function getHTML($filename, $php_params = array()) {
		if (!file_exists('views/'.$filename))
			die ('The view ' . $filename . ' doesn\'t exist');

		// If we got some params to be treated in php
		extract($php_params);
		
		ob_start();
		include('views/'.$filename);
		$content = ob_get_contents();
		ob_end_clean();

		return $content;
	} 
	
	/*
		Return a human readable file format from a number of bytes
	*/
	function formatBytes($bytes) {
		if ($bytes < 1024) return $bytes.' B';
		elseif ($bytes < 1048576) return round($bytes / 1024, 2).' KB';
		elseif ($bytes < 1073741824) return round($bytes / 1048576, 2).' MB';
		elseif ($bytes < 1099511627776) return round($bytes / 1073741824, 2).' GB';
		else return round($bytes / 1099511627776, 2).' TB';
	}
	
	/*
		Redirect the user to the specified URL
	*/
	function redirect($url) {
		header('Location: '.$url);
		exit();
	}
	
	/*
		Return the text for the index in the current language the user is using
	*/
	function getLang($index,$params) {	
		global $lang;
		
		$output = $lang[$index];
		
		foreach ($params as $key => $value) {
			$output = str_replace('%'.$key.'%',$value,$output);
		}
		
		return $output;
	}
	
	/*
		Return a message for a form success
	*/
	function displaySuccessMessage($index,$params = array()) {
		global $lang;
	
		return '<div class="success_message">'.getLang('form_success_'.$index,$params).'</div>';
	}
	
	/*
		Return a message for a form info
	*/
	function displayInfoMessage($index,$params = array()) {
		global $lang;
	
		return '<div class="info_message">'.getLang('form_info_'.$index,$params).'</div>';
	}

	/*
		Return a message for a form error
	*/
	function displayErrorMessage($index,$params = array()) {
		global $lang;
	
		$message = nl2br(getLang('form_error_'.$index,$params));
	
		if ($index == 'something_wrong_happened' && isset($params['message'])) {
			$message .= ' '.getCassandraMessage($params['message']);
		}
	
		return '<div class="error_message">'.$message.'</div>';
	}
	
	$current_page_title = 'Fresho';
	
	/*
		Get the currrent page title for the HTML page
	*/
	function getPageTitle() {
		global $current_page_title;
		
		return $current_page_title;
	}
		
	/*
		Return the number of seconds elapsed between the time start and time end
	*/
	function getQueryTime($time_start,$time_end) {
		return round($time_end - $time_start,4).'sec';
	}
	
?>